// 版权归 瞄你个汪 所有。使用本代码应遵守相关法律法规和Apache 2.0开源许可要求。
package top.geeke.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.val;
import org.apache.ibatis.annotations.Mapper;
import top.geeke.entity.SysUserRole;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统用户角色表 DAO接口
 * </p>
 * @author 瞄你个汪
 * @since 2024-05-01 12:00:00
 */
@Mapper
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {

    /**
     * 获取用户角色ids
     * @param userId 用户id
     * @return 角色ids
     */
    default List<Long> getRoleIds(Long userId) {
        val wrapper = new QueryWrapper<SysUserRole>().lambda()
                .eq(SysUserRole::getUserId, userId)
                .select(SysUserRole::getRoleId);

        List<SysUserRole> userRoles = selectList(wrapper);

        if (userRoles != null && !userRoles.isEmpty()) {
            return userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        }

        return null;
    }
}
